package com.atguigu.hellomp.Test;

import io.minio.BucketExistsArgs;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.*;

import org.joda.time.DateTime;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.DateTimeException;
import java.util.UUID;

/**
 * @author 张智勇
 * @date 2025年08月15日 11:50
 */

public class T1 {
    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        MinioClient minioClient = MinioClient.builder()
                .endpoint("http://192.168.100.100:9000")
                .credentials("minioadmin", "minioadmin")
                .build();


        BucketExistsArgs bucketExistsArgs = BucketExistsArgs.builder().bucket("lease-0528").build();
        boolean bucketExists = minioClient.bucketExists(bucketExistsArgs);
        System.out.println("================="+bucketExists+"=================");

        String bucketName = "lease-0528";

        String fileName = "桌面主题.jpg";
        //获取文件扩展名
        String filenameExtension = StringUtils.getFilenameExtension(fileName);
        System.out.println(filenameExtension);

        String uuid = UUID.randomUUID().toString().replace("-", "");
        String uuidExtension = uuid +"."+filenameExtension;
        System.out.println(uuidExtension);

        String date = new DateTime().toString("yyyy/MM/dd");
        System.out.println(date);

        //注意与putObjectArgs的区别
        UploadObjectArgs uploadObjectArgs = UploadObjectArgs.builder()
                .filename("C:/Users/张智勇/Desktop/图片/桌面主题.jpg")
                .bucket(bucketName)
                .object(date + uuidExtension)
                .build();

        //以上所有的代码都是为这一步做准备的
        //1.创建客户；2.构建上传对象的参数
        minioClient.uploadObject(uploadObjectArgs);
        String url = "http://192.168.100.100:9000/"+bucketName+"/"+date+uuidExtension;

        System.out.println(url);

    }


}
